clear   
set fredkey ###INSERT HERE###

* Create temp folder
* ==========================
mkdir "${dirTemp}/fred"

* Choose variables to obtain
* =================================
global fredKeys      "GDPDEF FEDFUNDS PIRIC A008RD3Q086SBEA OPHNFB    Y694RC1Q027SBEA CPIAUCSL GDPC1 PNFI GS1 GS2 HOANBS PRS85006173 AAA"
global fredVariables "GDPDEF FFR      PIRIC GDPDEFINV       laborProd RND             CPI      RGDP  NINV GS1 GS2 HOANBS laborShare  AAAyield"  
global nfredKeys: word count $fredKeys
assert $nfredKeys ==`:word count $fredVariables'

* Download Data
* =======================
forval i=1/$nfredKeys {
	clear 
	local thisVariable `: word `i' of $fredKeys'
	local thisVariableNewName `: word `i' of $fredVariables' 
	import fred `thisVariable'
	rename `thisVariable' `thisVariableNewName' 
	save ${dirTemp}/fred/`thisVariableNewName', replace 
}   

* Merge Data
* =================
clear all
gen time = . 
save ${dirTemp}/fred_data_monthly, replace    
save ${dirTemp}/fred_data_quarterly, replace   
 
foreach f of global fredVariables {  
	use ${dirTemp}/fred/`f', clear 
	gen the_date = daten  
	if `=`=the_date[2]' - `=the_date[1]'' > 35 {
		local freq = "q"
		local freq_long = "quarterly"
	}
	else {
		local freq = "m"
		local freq_long = "monthly"
	} 
	gen time = `freq'ofd(the_date) 
	drop daten the_date  
	merge 1:1 time using ${dirTemp}/fred_data_`freq_long', nogenerate update  
	save ${dirTemp}/fred_data_`freq_long', replace   
}   

* Format quarterly data
* ==============================
use ${dirTemp}/fred_data_quarterly, clear 
order time 
format time %tq
tsset time
rename time date_quarterly
drop datestr
compress
save ${dirTemp}/fred_data_quarterly, replace 

* Format monthly data
* ==============================
use ${dirTemp}/fred_data_monthly, clear 
order time 
format time %tm
tsset time
rename time date_monthly
drop datestr
compress
save ${dirTemp}/fred_data_monthly, replace  

* Merge averaged monthly to quarterly data
* ===================================================
gen date_quarterly = qofd(dofm(date_monthly))
format date_quarterly %tq

foreach v of var * { 
	local l`v' : variable label `v' 
} 

ds date*, not
collapse (mean) `r(varlist)', by(date_quarterly)
 
foreach v of var * { 
	label var `v' "(mean) `l`v''" 
}   

* Save all quarterly
* ======================
merge 1:1 date_quarterly using ${dirTemp}/fred_data_quarterly, nogen
compress
save ${dirTemp}/fred_data_quarterly, replace 

* Remove temp files
* ======================
shell rm -r "${dirTemp}/fred" 
